const Controller = require('../core/base_controller')

class UserController extends Controller {
  async get() {
  	const { ctx } = this
  	const client = ctx.grpc.com.jstide.grpc.user.userService
  	const { exception, user } = await client.getUserById({
      arg: ctx.query.id
    });
    if (exception) {
      this.exception(exception)
      return
    }
  	this.success(user);
  }

  async list() {
  	const { ctx } = this
  	const client = ctx.grpc.com.jstide.grpc.user.userService
  	const { exception, ...result } = await client.findUsers(this.page(ctx.query));
  	if (exception) {
      this.exception(exception)
      return
    }

    result.data = result.data.map(row => ({
    	...row,
    	valid: row.valid ? 1 : 0
    }))
    
  	this.grid(result)
  }
}

module.exports = UserController;
